<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js异步编程之宏微任务执行顺序</title>
  <style>
    li {
      margin-bottom: 10px;
    }
    li button {
      cursor: pointer;
    }
  </style>
</head>
<body>
  <ul>
    <li><button onclick="executeTask1()">测试</button> 优先执行主线程逻辑，然后执行从微任务队列中读取任务并执行，最后从执行宏任务队列中读取任务并执行</li>
    <li><button onclick="executeTask2()">测试</button> 如何理解setTimeout宏任务？定时器的延时即使不指定(或指定0)也最少需要4ms,定时器并非到指定时间就立刻执行，而是需要等待主线程执行完毕，多个定时器首先将延时小的加入宏任务队列，一旦主线程执行完毕，定时器将立即执行而无非等待</li>
  </ul>
  <script>
    function executeTask1() {
      // main -> success -> timeout
      setTimeout(() => {
        console.log('timeout')
      }, 0)
      Promise.resolve('success').then(res => console.log(res))
      console.log('main')
    }
    function executeTask2() {
      setTimeout(() => {
        console.log('timeout1')
      })
      setTimeout(() => {
        console.log('timeout2')
      }, 1000)
      for (let i = 0; i < 10000; i++) {
        console.log('')
      }
    }
  </script>
</body>
</html>